<template>
	<view>
		<page-head :title="option_title">
		</page-head>
		<view class="uni-padding-wrap uni-common-mt">
			<form @submit="formSubmit" @reset="formReset">
            <view class="uni-form-item uni-column">
                <view class="title"><text class="uni-form-item__title">产品条码</text></view>
                <view class="uni-list">
					<view class="uni-list-cell">
						<input class="uni-input" :value="code" name="code" placeholder="请输入产品条码"/>
						<uni-icons @click="scanCode" type="scan" size="30"></uni-icons>
					</view>
                    <!-- <text class="uni-icon uni-scan" @click="changePassword">&#xe568;</text> -->
				</view>
            </view>
				<view v-if="option_type == 1" class="uni-form-item uni-column">
					<view class="title">选择产品</view>
					<view class="uni-list">
						<view class="uni-list-cell">
							<view class="uni-list-cell-db">
								<picker @change="bindPickerChange" :value="index" :range="productlist" range-key="product_name">
									<view class="uni-input">{{productlist[index].product_name}}</view>
								</picker>
							</view>
						</view>
					</view>
				</view>
				<view class="uni-btn-v">
					<button form-type="submit" type="primary">保存</button>
					<button type="default" form-type="reset">重置</button>
				</view>
			</form>
		</view>
	</view>
</template>
<script>
	import graceChecker from "../../../common/graceChecker.js"
	import api from "@/utils/api.js";
	import * as ww from '@wecom/jssdk'
	import wxjssdk from 'wecomjsdk'
	export default {
		data() {
			return {
				index: 0,
				array: [{name:'中国'},{name: '美国'}, {name:'巴西'}, {name:'日本'}],
				productlist: [{
					id: 0,
					cat_name: "请选择产品"
				}],
				current_category_id: 0,
				code: "",
				option_type: 1
			}
		},
		computed: {
			option_title: function(){
				if(this.option_type == 1) return "产品入库";
				if(this.option_type == 2) return "产品出库";
				if(this.option_type == 3) return "销售产品";
			}
		},
		async onLoad(e) {
			console.log(e)
			this.option_type = e.option_type;
			
			this.fecthCategory();
			this.wecomCheck()
			// let currentUrl = window.location.href;
			// console.log(currentUrl);
			
			// var params = {
			// 	url : location.href.split('#')[0]
			// };
			
			// let {result} = await api.getAgentJsConfig(params)
			// // console.log(result)
			// console.log(ww)
			// var config = {
			// 	beta: true,
			// 	debug: true,
			//   corpId: result.corpid,       // 必填，当前用户企业所属企业ID
			//   agentId: result.agentId,                  // 必填，当前应用的AgentID
			//   jsApiList: result.jsApiList, // 必填，需要使用的JSAPI列表
			// };
			// config.getConfigSignature = function(){
			// 	return {
			// 		timestamp: result.timestamp, nonceStr: result.nonceStr, signature: result.signature
			// 	};
			// }
			// console.log(config);
			// ww.register(config)
	
			

			
		},
		methods: {
			async wecomCheck(){
				var params = {
					url : location.href.split('#')[0]
				};
				
				let {data} = await api.getAgentJsConfig(params);
				let corpJsConfig = await api.getCorpJsConfig(params);
				var config = {
				  corpId: data.corpid,       // 必填，当前用户企业所属企业ID
				  appId: data.appId,       // 必填，当前用户企业所属企业ID
				  agentId: data.agentid,  
				  jsApiList: corpJsConfig.data.jsApiList
				};
			
				config.getConfigSignature = function() {
					return { timestamp: corpJsConfig.data.timestamp, nonceStr: corpJsConfig.data.nonceStr, signature: corpJsConfig.data.signature };
				}
			
				ww.register(config)
			},
			async fecthCategory(){
				let { result } = await api.productList();
				console.log(result)
				this.productlist = result.data.list;
			},
			
			async scanCode(){
				console.log("this.globalData", this.globalData)
				console.log("扫码")
				try{
					let result = await ww.scanQRCode({
					  needResult: true,
					  scanType: ['qrCode', 'barCode']
					})
					console.log("--------------------------")
					console.log("qrcode-result", result)
					
					// 模拟扫码结果
					// let result = {};
					// result.errCode = 0;
					// result.resultStr = "12345678";
					
					
					
					if(result.errCode == 0){
						// 扫码成功
						// code = 
						console.log("qrcode-result = resultStr", result.resultStr)
						this.code = result.resultStr;
					}else{
						uni.showToast({
						    title: "扫码失败",
						    icon: 'none',
						    duration: 2000
						})
					}
				}catch(e){
					//TODO handle the exception
					uni.showToast({
					    title: "扫码失败",
					    icon: 'none',
					    duration: 2000
					})
				}				

			},
			
			bindPickerChange: function(e) {
				console.log(e)
				console.log('picker发送选择改变，携带值为：' + e.detail.value)
				this.index = e.detail.value
			},
			formSubmit: async function(e) {
				var formData = e.detail.value;
				console.log(formData)
				formData.product_id = this.productlist[this.index].id;
				formData.option_type = this.option_type;
				try{
					let { result } = await api.productStockAdd(formData);
					// console.log(result)
					uni.showToast({
						title: result.msg,
						icon: "none"
					})
					if(result.code == 500){
		
					}else{
						this.code = "";
					}
				}catch(e){
					//TODO handle the exception
				}
			},
			formReset: function(e) {
				console.log('清空数据')
			}
		}
	}
</script>

<style>
	.uni-form-item .title {
		padding: 20rpx 0;
	}
</style>
